package com.markerhub.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.dto.Echarts;
import com.markerhub.dto.VisitsRecordDto;
import com.markerhub.entity.SysRole;
import com.markerhub.entity.VisitsRecord;
import com.markerhub.service.VisitsRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.security.Principal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yaozhenbin
 * @since 2022-03-25
 */
@RestController
@Slf4j
@RequestMapping("/file")
public class VisitsRecordController extends BaseController {

    @Autowired
    VisitsRecordService visitsRecordService;

    /**
     * 来访登记
     *
     * @param visitsRecordDto 来访个人信息
     * @param
     */
    @PostMapping(value = "/upload", produces = "application/json;charset=UTF-8")
    public Result record(VisitsRecordDto visitsRecordDto, HttpServletRequest request) {
        MultipartFile file = null;
                boolean isMultipart = ServletFileUpload.isMultipartContent(request);
                if (isMultipart){
                         MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
                        file = multipartRequest.getFile("file");
                }
        return visitsRecordService.insetRecord(visitsRecordDto, file);
    }

    /**
     * 获取今日来访列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result getTodayList() {
        String today = String.format("%tF", new Date());
        Page<VisitsRecord> pageData = visitsRecordService.page(getPage(),
                new QueryWrapper<VisitsRecord>().eq("time", today).or().in("status",0,1)
        );
        return Result.succ(pageData);
    }

    /**
     * 受理
     *
     * @param
     * @return
     */
    @PostMapping("/accept")
    @PreAuthorize("hasAuthority('sys:visits:accept')")
    public Result accept(@Validated @RequestBody VisitsRecord visitsRecord) {
        UpdateWrapper<VisitsRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", visitsRecord.getId());
        VisitsRecord record = new VisitsRecord();
        record.setStatus(1);
        record.setRemarks(visitsRecord.getRemarks());
        boolean res = visitsRecordService.update(record, updateWrapper);
        return Result.succ(res);
    }

    /**
     * 驳回
     *
     * @param visitsRecord
     * @return
     */
    @PostMapping("/reject")
    @PreAuthorize("hasAnyAuthority('sys:visits:reject')")
    public Result reject(@Validated @RequestBody VisitsRecord visitsRecord) {
        UpdateWrapper<VisitsRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", visitsRecord.getId());
        VisitsRecord record = new VisitsRecord();
        record.setStatus(3);
        record.setRemarks(visitsRecord.getRemarks());
        boolean res = visitsRecordService.update(record, updateWrapper);
        return Result.succ(res);
    }

    /**
     * 获取来访处理列表
     *
     * @return
     */
    @GetMapping("/handle")
    @PreAuthorize("hasAnyAuthority('sys:visits:manage')")
    public Result handleList() {
        Page<VisitsRecord> pageData = visitsRecordService.page(getPage(),
                new QueryWrapper<VisitsRecord>().in("status",  5)
        );
        return Result.succ(pageData);
    }

    /**
     * 开始处理
     *
     * @param id
     * @return
     */
    @GetMapping("/manage")
    @PreAuthorize("hasAuthority('sys:visits:handle')")
    public Result handle(String id, Principal principal) {
        String today = String.format("%tF", new Date());
        UpdateWrapper<VisitsRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        VisitsRecord record = new VisitsRecord();
        record.setRemarks("案件已开始处理");
        record.setStatus(4);
        record.setHandler(principal.getName());
        record.setStart(today);
        boolean res = visitsRecordService.update(record, updateWrapper);
        return Result.succ(res);
    }

    /**
     * 结案
     *
     * @param visitsRecordDto
     * @return
     */

    @PostMapping(value = "/finish", produces = "application/json;charset=UTF-8")

    @PreAuthorize("hasAuthority('sys:visits:finish')")
    public Result finish(VisitsRecordDto visitsRecordDto, HttpServletRequest request,Principal principal) {
        MultipartFile file = null;
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart){
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
            file = multipartRequest.getFile("file");
        }
        return visitsRecordService.updateRecord(visitsRecordDto, file,principal);
    }

    /**
     * 归档
     *
     * @param
     * @return
     */
    @PostMapping("/achieve")
    @PreAuthorize("hasAuthority('sys:visits:achieve')")
    public Result handleAchieve(@Validated @RequestBody VisitsRecord visitsRecord) {
        String today = String.format("%tF", new Date());
        UpdateWrapper<VisitsRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", visitsRecord.getId());
        VisitsRecord record = new VisitsRecord();
        record.setStatus(6);
        record.setAchieve(today);
        record.setRemarks(visitsRecord.getRemarks());
        boolean res = visitsRecordService.update(record, updateWrapper);
        return Result.succ(res);
    }

    /**
     * 获取来访归档列表
     *
     * @return
     */
    @GetMapping("/achievelist")
    public Result achieveList() {
        Page<VisitsRecord> pageData = visitsRecordService.page(getPage(),
                new QueryWrapper<VisitsRecord>().eq("status", 6)
        );
        return Result.succ(pageData);
    }

    /**
     * 来访信息管理列表
     *
     * @return
     */
    @GetMapping("/all")
    public Result all() {
        Page<VisitsRecord> pageData = visitsRecordService.page(getPage(),
                new QueryWrapper<VisitsRecord>().select("*")
        );
        return Result.succ(pageData);
    }

    /**
     * 来访转发
     *
     * @param id
     * @param organization
     * @return
     */
    @GetMapping("/forward")
    @PreAuthorize("hasAnyAuthority('sys:visits:forward')")
    public Result forward(@RequestParam("id") String id, @RequestParam("organization") String organization) {
        UpdateWrapper<VisitsRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        VisitsRecord record = new VisitsRecord();
        record.setStatus(2);
        record.setOrganization(organization);
        record.setRemarks("已转发至相关部门处理");
        boolean res = visitsRecordService.update(record, updateWrapper);
        return Result.succ(res);

    }

    /**
     * 获取指定机构列表
     *
     * @param organization
     * @return
     */
    @GetMapping("/orgList")

    public Result orgList(@RequestParam("organization") String organization) {

        Page<VisitsRecord> pageData = visitsRecordService.page(getPage(),
                new QueryWrapper<VisitsRecord>().eq("organization", organization));

        return Result.succ(pageData);
    }

    /**
     * 获取默认机构列表
     *
     * @return
     */
    @GetMapping("/org")
    @PreAuthorize("hasAnyAuthority('sys:city:list')")
    public Result Org() {
        Page<VisitsRecord> pageData = visitsRecordService.page(getPage(),
                new QueryWrapper<VisitsRecord>().in("organization", "惠州市信访局", "惠城区信访局")
        );
        return Result.succ(pageData);
    }

     @GetMapping("/totle")
    public Result getTotle(){
        int []totle=visitsRecordService.getTotle();
        return Result.succ(totle);
     }
    @GetMapping("/yeartotle")
    public Result getYearTotle(){
        visitsRecordService.getYearTotle();
        return Result.succ( visitsRecordService.getYearTotle());
    }
    @GetMapping("/monthtotle")
    public Result getMonthTotle(){
        visitsRecordService.getMonthTotle();

        return Result.succ(visitsRecordService.getMonthTotle());
    }
}
